package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.mapper.SendMapper;
import com.aaa.mrcai.pojo.entity.EmpInfo;
import com.aaa.mrcai.pojo.entity.Notify;
import com.aaa.mrcai.pojo.entity.Role;
import com.aaa.mrcai.pojo.vo.SendVO;
import com.aaa.mrcai.query.SendQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.SendService;
import com.aaa.mrcai.util.GoEasyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * @author meowyf
 * @version 1.0
 * @Name Z_MJ
 * @date 2021/12/13 16:43:28
 */
@Service
public class SendServiceImpl implements SendService {
    @Autowired
    private SendMapper sendMapper;
    @Autowired
    private HttpSession session;
    @Override
    public AjaxResult list(SendQuery sendQuery) {
        List<Notify> notify = sendMapper.list(sendQuery);
        Long totalCount = sendMapper.querySendTotalCount(sendQuery);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("empListAll",notify);
        map.put("empNumber",totalCount);

        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult listScontend(int id) {
        Notify notify = sendMapper.listScontend(id);
        System.out.println("//////////////////"+notify);
        return AjaxResult.success(notify);
    }

    @Override
    public AjaxResult listRole() {
        List<Role> roles = sendMapper.listRole();
        return AjaxResult.success(roles);
    }

    @Override
    public AjaxResult deleteSend(int id) {
        int i = sendMapper.deleteSend(id);
        if (i>0){
            return AjaxResult.success();
        }
        return AjaxResult.defeated();
    }

    @Override
    public AjaxResult addNotify(SendVO sendVO) {
        System.out.println(sendVO);
        EmpInfo emp = (EmpInfo) session.getAttribute("emp");
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        sendVO.setSdate(date.format(formatter));
        sendVO.setRid(emp.getEmpRole().getId());
        int i = sendMapper.addNotify(sendVO);
        System.out.println("]]]]]]]]]]]]]]]]]]]"+i);
        int i1 = sendMapper.addReception(sendVO);
        GoEasyUtil.goEasy(sendVO.getSdate());
        return AjaxResult.success();
    }
}
